﻿using EmoneyWeiXinCommon;
using EmoneyWeiXinDAL;
using EmoneyWeiXinEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using tenpayApp;

namespace EmoneyWeiXinBLL
{
    public class DailyTaskBLL
    {
        public static Object lockObj = new object();
        public static int AddOneDailyTask(DailyTaskEntity entity)
        {
            return DailyTaskDAL.AddOneDailyTask(entity);
        }

        public static void AddOneDailyTaskForThread(Object obj)
        {
            DailyTaskEntity entity = obj as DailyTaskEntity;
            DailyTaskDAL.AddOneDailyTask(entity);
        }


        public static List<string> GetSignDateByOpenId(string openid)
        {
            return DailyTaskDAL.GetSignDateByOpenId(openid);
        }

        public static GoldUserEntity GetGoldUser(string openid)
        {
            return DailyTaskDAL.GetGoldUser(openid);
        }

        public static int AddDailyTaskZjJilu(string openid, string gameType, string winningType, int payGold, double getMoney, out int syGold)
        {
            return DailyTaskDAL.AddDailyTaskZjJilu(openid, gameType, winningType, payGold, getMoney, out syGold);
        }


        public static string TiXian(string openid, string weixinId, SendRedPackConfig srpConfig)
        {
            SqlTransaction tran = SQLHelper.BeginTransAction(Config.EmoneyWeiXin);
            lock (lockObj)
            {
                try
                {

                    string pre_mch_billno = DateTime.Now.ToString("HHmmss") + TenpayUtil.BuildRandomStr(10);
                    pre_mch_billno = srpConfig.mch_id + pre_mch_billno;
                    string t_order_no = pre_mch_billno;
                    string sendMessage = string.Empty;
                    double t_money = 0;
                    DailyTaskDAL.GetAllMoney(openid, t_order_no, tran, out t_money);
                    if(t_money==-11)
                    {
                        tran.Rollback();
                        return "每天最多提现3次，明天再来提吧！";
                    }
                    if (t_money < 1)
                    {
                        tran.Rollback();
                        return "你的资产不足一元，不能提取";
                    }
                    
                    string sendResult = "";
                    if (System.Configuration.ConfigurationManager.AppSettings["isTest"] != "1")
                    {
                        sendResult = WeiXinPayBLL.sendRedBag(srpConfig, openid, t_money * 100, t_order_no, out sendMessage);
                    }
                    if (sendResult == "")
                    {
                        tran.Commit();
                        return "提取成功！\n我们已以红包的形式发送给您,请回到公众号查看\n本次提现" + t_money + "元";
                    }
                    else
                    {

                        tran.Rollback();
                        return "提现失败，请稍后重试！";
                    }
                }
                catch (Exception)
                {

                    tran.Rollback();
                    return "提现失败，请稍后重试！";
                }
            }
        }


        public static List<DailyTaskEntity> GetDailyTaskByPage(string openid, int page, out int totalPage)
        {
            return DailyTaskDAL.GetDailyTaskByPage(openid, page, out totalPage);
        }

        public static List<GoldUserTradingRecordEntity> GetTradingRecordListByPage(string openid, int page, out int total)
        {
            return DailyTaskDAL.GetTradingRecordListByPage(openid, page, out total);
        }

        public static DataTable GetUserMainInfo(string openid)
        {
            return DailyTaskDAL.GetUserMainInfo(openid);
        }

        public static DataTable GetTjData()
        {
            return DailyTaskDAL.GetTjData();
        }
    }
}
